<?php

namespace App\Http\Controllers\Member;

use App\Model\Globals;
use App\Models\Archive\Archive;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class masterGlobalController extends Controller
{
    //
    public function index()
    {
        if($input = Input::except('_token')){
            if(!empty($input['id'])){
                //首页id处理
                $input['mod_top1'] = str_replace('，',',',$input['mod_top1']);
                $top1 = explode(',',$input['mod_top1']);
                foreach($top1 as $v){
                    if(!Archive::where('id',$v)->ofPattern('review')->first()){
                        return back()->with('msg','置顶模块1ID为'.$v.'未审核！');
                    };
                }
                $input['mod_top2'] = str_replace('，',',',$input['mod_top2']);
                $top2 = explode(',',$input['mod_top2']);
                foreach($top2 as $v){
                    if(!Archive::where('id',$v)->ofPattern('review')->first()){
                        return back()->with('msg','置顶模块2ID为'.$v.'未审核！');
                    };
                }
                $input['mod_anchor'] = str_replace('，',',',$input['mod_anchor']);
                $input['mod_video'] = str_replace('，',',',$input['mod_video']);
                $video = explode(',',$input['mod_top2']);
                foreach($video as $v){
                    if(!Archive::where('id',$v)->ofPattern('review')->first()){
                        return back()->with('msg','视频模块ID为'.$v.'未审核！');
                    };
                }
                $input['mod_article1'] = str_replace('，',',',$input['mod_article1']);
                $article1 = explode(',',$input['mod_top2']);
                foreach($article1 as $v){
                    if(!Archive::where('id',$v)->ofPattern('review')->first()){
                        return back()->with('msg','文章模块1ID为'.$v.'未审核！');
                    };
                }
                $input['mod_article2'] = str_replace('，',',',$input['mod_article2']);
                $article2 = explode(',',$input['mod_top2']);
                foreach($article2 as $v){
                    if(!Archive::where('id',$v)->ofPattern('review')->first()){
                        return back()->with('msg','文章模块2ID为'.$v.'未审核！');
                    };
                }
                $input['mod_gallery'] = str_replace('，',',',$input['mod_gallery']);
                $gallery = explode(',',$input['mod_top2']);
                foreach($gallery as $v){
                    if(!Archive::where('id',$v)->ofPattern('review')->first()){
                        return back()->with('msg','图集模块ID为'.$v.'未审核！');
                    };
                }

                $input['search_key'] = str_replace('，',',',$input['search_key']);
                if(Globals::where('id',$input['id'])->update($input)){
                    return back()->with('msg','设置修改成功！');
                }else{
                    return back()->with('msg','设置修改失败！');
                }
            }else{
                return back()->with('msg','非法操作！');
            }
        }else{
            $global = Globals::first();
            return view('member.masterGlobal',compact('global'));
        }
    }
}
